package com.syntaxtree.trees;

import com.syntaxtree.constant.ModifierCharacter;
import com.syntaxtree.constant.Other;
import com.syntaxtree.constant.Visibility;

public class VarTree extends BaseTree {
	public String VarName;
	public String VarType;
	public String VarVisibility;
	public boolean HasValue;
	/** 运行时才进行赋值 */
	public Object VarValue;
	public boolean IsFinal;
	public boolean IsStatic;
	public boolean IsVolatile;
	public boolean IsTtransient;
	
	public VarTree(String code) {
		super(code);
		initLocalVariable();
	}
	
	private void initLocalVariable() {
		this.VarName=getVarName(this.Code);
		this.VarType=getVarType(this.Code);
		
		this.VarVisibility=Other.Empty;
		this.HasValue=false;
		if(this.Code.contains("=")) {
			this.HasValue=true;
			//this.VarValue=this.Code.split("=")[1].replace(";",Other.Empty).trim();
		}
		
		String []split=this.Code.split(Other.SPACE);
		for (int i = 0; i < split.length; i++) {
			if (split[i].equals(Visibility.PUBLIC))
				this.VarVisibility=Visibility.PUBLIC;
			else if (split[i].equals(Visibility.PROTECTED))
				this.VarVisibility=Visibility.PROTECTED;
			else if (split[i].equals(Visibility.PRIVATE))
				this.VarVisibility=Visibility.PRIVATE;
			else if (split[i].equals(Visibility.DEFAULT))
				this.VarVisibility=Visibility.DEFAULT;
			if (split[i].equals(ModifierCharacter.FINAL))
				this.IsFinal=true;
			if (split[i].equals(ModifierCharacter.STATIC))
				this.IsStatic=true;
			if (split[i].equals(ModifierCharacter.VOLATILE))
				this.IsVolatile=true;
			if (split[i].equals(ModifierCharacter.TRANSIENT))
				this.IsTtransient=true;
		}
	}
	
	private String getVarName(String code) throws IllegalArgumentException {
		if(code.contains("=")) {
			String []split=code.split("=");
			split=split[0].trim().split(Other.SPACE);
			return split[split.length-1];
		}else {
			code=code.replace(";", Other.Empty).trim();
			String[] split=code.trim().split(Other.SPACE);
			return split[split.length-1];
		}
	}
	
	private String getVarType(String code) throws IllegalArgumentException {
		String []split=code.split(getVarName(code));
		String pattern=String.format("(?:%s|%s|%s|%s|%s|%s|%s|%s)\\s", 
				Visibility.PRIVATE,
				Visibility.PUBLIC,
				Visibility.PROTECTED,
				Visibility.DEFAULT,
				ModifierCharacter.TRANSIENT,
				ModifierCharacter.STATIC,
				ModifierCharacter.FINAL,
				ModifierCharacter.VOLATILE);
		return split[0].replaceAll(pattern,Other.Empty).trim();
	}
	
	public void showInfo() {
		System.out.println(String.format("源代码:%s", this.Code));
		System.out.println(String.format("变量名:%s", this.VarName));
		System.out.println(String.format("变量类型:%s", this.VarType));
		System.out.println(String.format("变量可见性:%s", this.VarVisibility));
		System.out.println(String.format("变量是否有值:%s", this.HasValue));
		System.out.println(String.format("变量值:%s", this.VarValue));
		System.out.println(String.format("IsFinal:%s", this.IsFinal));
		System.out.println(String.format("IsStatic:%s", this.IsStatic));
		System.out.println(String.format("IsVolatile:%s", this.IsVolatile));
		System.out.println(String.format("IsTtransient:%s", this.IsTtransient));
	}
}
